﻿using Services.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;

namespace Ionic_Server.Common
{
    public class BasicAuthorizeAttribute: AuthorizeAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            var auth = actionContext.Request.Headers.Authorization;
            if (auth != null && auth.ToString().IndexOf("-") > 0)//必须包含-
            {
                string[] auths = auth.ToString().Split('-');
                string clientToken = auths[1];
                string uid = auths[0];

                //验证Token
                string serverToken =  UserTokenManager.GetToken(uid).Token;
                if (string.Equals(serverToken, clientToken))
                {
                    IsAuthorized(actionContext);
                }
                else
                {
                    HandleUnauthorizedRequest(actionContext);
                }
            }
            else
            {
                HandleUnauthorizedRequest(actionContext);
            }
        }
    }
}